package com.ulook.lookplanet.ui.notifications.UserDetail;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;
import com.ulook.lookplanet.R;
import com.ulook.lookplanet.common.AppServer;
import com.ulook.lookplanet.data.model.BaseUser;
import com.ulook.lookplanet.data.model.TCornUser;
import com.ulook.lookplanet.data.model.TUser;
import com.ulook.lookplanet.ui.home.VUserInfo;
import com.ulook.lookplanet.ui.notifications.VMsgUser;
import com.ulook.lookplanet.ui.notifications.chat.ChatActivity;
import com.ulook.lookplanet.widget.RoundImageView;

public class UserDetailActivity extends AppCompatActivity {

    private UserDetailViewModel viewModel;

    private RoundImageView head;
    private TextView username;
    private TextView level;
    private TextView planet;
    private TextView fans;
    private TextView con_topics;
    private TextView con_user;
    private TextView username2;
    private TextView id;
    private TextView age;
    private TextView gender;
    private TextView star_sign;
    private TextView city;
    private TextView hobby;
    private TextView signature;
    private QMUIRoundButton cornUserBt;
    private QMUIRoundButton charBt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_detail);

        viewModel = new ViewModelProvider(this).get(UserDetailViewModel.class);
        viewModel.getToastText().observe(this, s -> {
            Toast.makeText(UserDetailActivity.this, s, Toast.LENGTH_SHORT).show();
        });
        viewModel.getUserInfo().observe(this, vUserInfo -> {
            setUserInfoView(vUserInfo); // 展示用户信息
        });
        viewModel.getCornUser().observe(this, tCornUser -> {
            isCornUser(tCornUser);
        });

        Intent intent = getIntent();
        int userId = intent.getIntExtra("user", 0);
        int currUser = intent.getIntExtra("currUser", 0);
        viewModel.setUserId(userId);
        viewModel.setCurrUser(currUser);

        initTopBar();
        initData();
    }

    private void initTopBar(){
        QMUITopBarLayout topBar = findViewById(R.id.user_detail_bar);
        topBar.setTitle("用户详情");
        topBar.addLeftBackImageButton().setOnClickListener(view -> {
            finish();
        });
    }

    private void initData(){
        initUserInfo();
        if(!viewModel.initUserInfo()){
            finish();
        }

        cornUserBt = findViewById(R.id.user_detail_corn_bt);
        charBt = findViewById(R.id.user_detail_char_bt);
        if(viewModel.getUserId() == viewModel.getCurrUser()){
            cornUserBt.setVisibility(View.GONE);
            charBt.setVisibility(View.GONE);
        }else {
            // 点击关注
            cornUserBt.setOnClickListener(view -> {
                viewModel.beCornUser();
            });
            viewModel.isCornUser();
            // 点击聊天
            charBt.setOnClickListener(view -> {
                //  跳转聊天界面
                VMsgUser msgUser = new VMsgUser();
                msgUser.setFromuser(viewModel.getUserInfo().getValue().getId());
                msgUser.setFromname(viewModel.getUserInfo().getValue().getName());
                msgUser.setFromheadimg(viewModel.getUserInfo().getValue().getHeadimg());
                msgUser.setTouser(viewModel.getCurrUser());
                Intent intent = new Intent(UserDetailActivity.this, ChatActivity.class);
                intent.putExtra("VMsgUser", msgUser);
                startActivity(intent);
            });
        }
    }

    /**
     * 是否关注
     */
    private void isCornUser(TCornUser cornUser){
        if(cornUser == null){
            cornUserBt.setText(R.string.user_detail_corn);
        }else {
            cornUserBt.setText(R.string.user_detail_corn_ed);
        }
    }

    /**
     * 初始化用户信息界面
     */
    private void initUserInfo(){
        head = findViewById(R.id.user_detail_img);
        head.setImageResource(R.mipmap.user_head_img);
        username = findViewById(R.id.user_detail_name);
        username.setText(R.string.user_name);
        level = findViewById(R.id.user_detail_level);
        level.setText(R.string.level);
        planet = findViewById(R.id.user_detail_planet);
        planet.setText(R.string.user_planet);
        fans = findViewById(R.id.user_detail_fans);
        fans.setText(R.string.fans);
        con_topics = findViewById(R.id.user_detail_con_topics);
        con_topics.setText(R.string.con_topics);
        con_user = findViewById(R.id.user_detail_con_user);
        con_user.setText(R.string.con_user);
        username2 = findViewById(R.id.user_detail_name2);
        username2.setText(R.string.user_name2);
        id = findViewById(R.id.user_detail_id);
        id.setText(R.string.user_id);
        age = findViewById(R.id.user_detail_age);
        age.setText(R.string.age);
        gender = findViewById(R.id.user_detail_gender);
        gender.setText(R.string.gender);
        star_sign = findViewById(R.id.user_detail_star_sign);
        star_sign.setText(R.string.star_sign);
        city = findViewById(R.id.user_detail_city);
        city.setText(R.string.city);
        hobby = findViewById(R.id.user_detail_hobby);
        hobby.setText(R.string.hobby);
        signature = findViewById(R.id.user_detail_signature);
        signature.setText(R.string.signature);
    }

    /**
     * 展示用户信息
     */
    private void setUserInfoView(VUserInfo vUserInfo){
        if(!"".equals(vUserInfo.getHeadimg()) && vUserInfo.getHeadimg() != null){
            head.setImageURL(AppServer.HEAD_IMG_PATH + vUserInfo.getHeadimg());
        }
        username.append(vUserInfo.getName());
        level.append(String.valueOf(vUserInfo.getLv()));
        planet.append(vUserInfo.getPlanetname());
        fans.append(String.valueOf(vUserInfo.getFans()));
        con_topics.append(String.valueOf(vUserInfo.getComus()));
        con_user.append(String.valueOf(vUserInfo.getCornusers()));
        username2.append(vUserInfo.getName());
        id.append(String.valueOf(vUserInfo.getId()));
        age.append(String.valueOf(vUserInfo.getAge()));
        gender.append(vUserInfo.getGender());
        star_sign.append(vUserInfo.getStarsign());
        city.append(vUserInfo.getCity());
        hobby.append(vUserInfo.getHobby());
        signature.append(vUserInfo.getSignature());
    }
}